const { tool,api,time } = getApp();
Page({
    data: {
        swiper: [
            'https://file.my2171.com/img/bishe/1679444672000.png',
            'https://file.my2171.com/img/bishe/1679444700000.png',
            'https://file.my2171.com/img/bishe/1679444714000.png'
        ],                      // 轮播图
        types: [],              // 课题分类
        type_id: '',            // 当前分类 id
        page: 0,                // 页码
        pagesize: 10,           // 单页数据量
        year: time.getYear(),   // 当前年份
        classList: [],          // 课题列表
        isLoding: false,        // 是否可以上拉加载
    },

    // 页面跳转
    jump(e) {
        let { type } = e.currentTarget.dataset;
        switch (type * 1) {
            case 0: api.navigateTo('/pages/class/search/index'); break;
        }
    },

    // 切换分类
    tabChange(e) {
        let { index } = e.detail;
        let type_id = this.data.types[index].id;
        this.setData({ type_id,page: 0,classList: [] });
        this.getClassList();
    },

    // 获取课题列表
    getClassList() {
        let { type_id,year,page,pagesize,classList } = this.data;
        tool.getTopic({ type_id,year,page,pagesize }).then(list => {
            classList.push(...list);
            this.setData({ classList,isLoding: list.length >= pagesize });
        })
    },

    // 监听页面加载
    onLoad(options) {
        tool.getTypes().then(types => {
            if (types.length) {
                let type_id = types[0].id;
                this.setData({ types,type_id });
                this.getClassList();
            }
        })
    },

    // 上拉加载
    onReachBottom() {
        let { isLoding,page } = this.data;
        if (isLoding) {
            page++;
            this.setData({ page })
            this.getClassList();
        } else api.showToast('暂无更多数据','none');
    },

})